package org.sunrise.game.game.service;

import org.sunrise.game.game.human.HumanObject;
import org.sunrise.game.game.human.HumanObjectManger;
import org.sunrise.game.game.proto.ChatProto;
import org.sunrise.game.game.proto.TopicProto;
import org.sunrise.game.rpc.annotation.RpcMethod;
import org.sunrise.game.rpc.annotation.RpcService;
import org.sunrise.game.rpc.service.BaseService;

import java.util.ArrayList;
import java.util.List;

@RpcService
public class ChatRpcListenService extends BaseService {
    public ChatRpcListenService(String nodeId) {
        super(nodeId);
    }

    @RpcMethod
    public void onChat(String humanId, int type, String message, long time) {
        for (HumanObject humanObject : HumanObjectManger.getHumanObjects()) {
            humanObject.sendMsg(TopicProto.TOPIC.TOPIC_TYPE_CHAT_VALUE, ChatProto.FROM_SERVER.S2C_Chat_VALUE, ChatProto.MS2C_Chat.newBuilder().setId(humanId).setMsg(message).setTime(time));
        }
    }

    @RpcMethod
    public void onHistory(String humanId, int type, List<String> ids, List<String> msgs, List<Long> times) {
        HumanObject humanObject = HumanObjectManger.getHumanObject(humanId);
        if (humanObject == null) return;
        ChatProto.MS2C_History.Builder newedBuilder = ChatProto.MS2C_History.newBuilder();
        for (int i = 0; i < ids.size(); i++) {
            newedBuilder.addHistory(ChatProto.MS2C_Chat.newBuilder().setId(ids.get(i)).setMsg(msgs.get(i)).setTime(times.get(i)));
        }

        humanObject.sendMsg(TopicProto.TOPIC.TOPIC_TYPE_CHAT_VALUE, ChatProto.FROM_SERVER.S2C_History_VALUE, newedBuilder);
    }
}